/*
 * CPassiveConnect.cpp
 *
 *  Created on: 2011-9-10
 *      Author: stargui
 */

#include "CPassiveConnect.h"


CPassiveConnect::CPassiveConnect()
{
	_listenTcpSocket 	= -1;
	_listenPort	   	= 9000;
	_resetnet			= 0;
	for (int i = 0; i < MAX_PASSIVE_THREADS; i++)
	{
		_passiveThread[i] = new CPassiveThread();
	}
	_sessionManager	= NULL;
}

CPassiveConnect::~CPassiveConnect()
{
	for (int i = 0; i < MAX_PASSIVE_THREADS; i++)
	{
		if(_passiveThread[i])
		{
			delete _passiveThread[i];
			_passiveThread[i]		= NULL;
		}
	}
}

CPassiveThread *CPassiveConnect::GetMinimumLoads()
{
	CPassiveThread *thread = _passiveThread[0];
	int minimum = _passiveThread[0]->GetCurrentLoads();
	for (int i = 1; i < MAX_PASSIVE_THREADS; i++)
	{
		if (_passiveThread[i]->GetCurrentLoads() < minimum)
		{
			minimum = _passiveThread[i]->GetCurrentLoads();
			thread = _passiveThread[i];
		}
	}
	return thread;
}

int CPassiveConnect::SetServiceParameter(unsigned short listenPort)
{
	printf("--SetServiceParameter-oldport:[%d]-newport:[%d]---\n",_listenPort,listenPort);
	if ((_listenTcpSocket <= 0) || (listenPort != _listenPort))
	{
		_resetnet				= 0;
		_listenPort 			= listenPort;
	}
	return 0;
}

bool CPassiveConnect::TaskSchedule()
{
	if(!_resetnet)
	{
		SetRelease();
		if (_listenTcpSocket <= 0)
		{
			_listenTcpSocket = CPSocketUtils::OpenTcpSocket();
			CPSocketUtils::SetSocketBuffer(_listenTcpSocket, 128 * 1024, 128 * 1024);
			CPSocketUtils::SetTcpSocketTimeout(_listenTcpSocket, 30, 10, 3);
			CPSocketUtils::ListenTcpSocket(_listenTcpSocket, _listenPort);
			CPSocketUtils::SetSockLingerAttr(_listenTcpSocket,1,2);
			if(_listenTcpSocket <= 0)
			{
				printf("-------create-listen-socket--failed!------\n");
				CPSelectUtils::mSleep(20);
				return false;
			}
			DEBUG_WARNING("---create-listen-socket--success!-_listenTcpSocket:[%d]-----\n",_listenTcpSocket);
		}
		_resetnet			= 1;
	}

	//����CPassivethread���Ƕ������̴߳��?�������̺߳�Ϊһ��
	for (int i = 0; i < MAX_PASSIVE_THREADS; i++)
	{
		_passiveThread[i]->ThreadFuncImpl();
	}

	if(_listenTcpSocket <= 0)
	{
		CPSelectUtils::mSleep(100);
		return false;
	}

	int remote = -1;
	remote	 	= CPSocketUtils::AcceptTcpSocket(_listenTcpSocket, 50000);
	if (remote > 0)
	{
		CPSocketUtils::SetNonBlockSocket(remote);
		CPSocketUtils::SetTcpSocketTimeout(remote, 30, 10, 3);
		CPSocketUtils::SetSockLingerAttr(remote,1,2);
		GetMinimumLoads()->OnSocketConnect(remote);
		printf("succsee socket is %d \n",remote);
	}
	else if(remote == -1)
	{
		SetRelease();
		CPSelectUtils::mSleep(100);
		printf("-accept failed! need create new listen-\n");
		return false;
	}

	return true;
}

void CPassiveConnect::ResetPassiveNet()
{
	_resetnet			= 0;
}

void CPassiveConnect::SetRelease()
{
	for (int i = 0; i < MAX_PASSIVE_THREADS; i++)
	{
		_passiveThread[i]->SetReleaseSockets();
	}
 	CSessionManager::GetInstance()->ResetSession();
	CPSelectUtils::mSleep(100);
	CPSocketUtils::CloseSocket(_listenTcpSocket);
	_listenTcpSocket = -1;
}

void CPassiveConnect::SetClientNo(unsigned int no)
{
	for (int i = 0; i < MAX_PASSIVE_THREADS; i++)
	{
		_passiveThread[i]->SetClientNo(no);
	}
}
